package com.qq.googleplay.ui.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.haarman.listviewanimations.itemmanipulation.OnDismissCallback;
import com.haarman.listviewanimations.itemmanipulation.SwipeDismissAdapter;
import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.qq.googleplay.base.BaseFragment;
import com.qq.googleplay.base.BaseHolder;
import com.qq.googleplay.base.LoadingPager;
import com.qq.googleplay.base.SuperBaseAdapter;
import com.qq.googleplay.bean.SubjectInfoBean;
import com.qq.googleplay.factory.ListViewFactory;
import com.qq.googleplay.http.Protocol.SubjectProtocol;
import com.qq.googleplay.ui.holder.SubjectHolder;

import java.util.List;

/**
 * ============================================================
 * 版 权 ： Google互联网有限公司版权所有 (c) 2016
 * 作 者 : 陈冠杰
 * 版 本 ： 1.0
 * 创建日期 ：2016/3/20 19:40
 * 描 述 ： 专题页面
 * 修订历史 ：
 * ============================================================
 **/
public class SubjectFragment extends BaseFragment implements OnDismissCallback {


    private SubjectProtocol mSubjectProtocol;
    private List<SubjectInfoBean> mData;

    private SubjectAdapter mSubjectAdapter;

    @Override
    protected View initSuccessView() {

        /*AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(mAdapter);
        animAdapter.setAbsListView(mCurrentListView);
		mCurrentListView.setAdapter(animAdapter);*/


        ListView listView = ListViewFactory.createListView();
        listView.setBackgroundColor(Color.parseColor("#eaeaea"));
        mSubjectAdapter = new SubjectAdapter(listView, mData);

        /*AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(mSubjectAdapter);
        animAdapter.setAbsListView(listView);
        listView.setAdapter(animAdapter);*/


        SwingBottomInAnimationAdapter animationAdapter =
                new SwingBottomInAnimationAdapter(new SwipeDismissAdapter(mSubjectAdapter, this));
        animationAdapter.setAbsListView(listView);
        listView.setAdapter(animationAdapter);

        return listView;
    }

    @Override
    protected LoadingPager.LoadedResult initData() {
        mSubjectProtocol = new SubjectProtocol();
        try {
            mData = mSubjectProtocol.loadData(0);
            return checkState(mData);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return LoadingPager.LoadedResult.ERROR;
        }
    }

    @Override
    public void onDismiss(AbsListView listView, int[] reverseSortedPositions) {
        for (int position : reverseSortedPositions) {
            mData.remove(position);
            mSubjectAdapter.notifyDataSetChanged();
            toast("第" + (position + 1) + "个item被删除了");
        }
    }

    class SubjectAdapter extends SuperBaseAdapter<SubjectInfoBean> {
        public SubjectAdapter(AbsListView listView, List<SubjectInfoBean> dataSource) {
            super(listView, dataSource);
        }

        @Override
        public BaseHolder<SubjectInfoBean> getSpecialHolder(int position) {
            return new SubjectHolder(mActivity);
        }

        @Override
        public List<SubjectInfoBean> onLoadMore() throws Throwable {
            return mSubjectProtocol.loadData(mData.size());
        }
    }
}
